﻿using System.IO;
using System.Windows;
#if WPF
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.Foundation;
#endif

namespace Eternal.DataVisualization
{
    public abstract class Performance : AxisType
    {
        protected Image bitmapImage = new Image();
        protected WriteableBitmap writeableBitmap { get; set; }
        protected byte[] FastBuffer { get; set; }
        protected Stream FastRenderStream { get; set; }
        private bool isImageAdded;

#if WPF_UN

        public PerformanceType RenderPerformance
        {
            get { return (PerformanceType)GetValue(RenderPerformanceProperty); }
            set { SetValue(RenderPerformanceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RenderPerformance.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RenderPerformanceProperty =
            DependencyProperty.Register("RenderPerformance", typeof(PerformanceType), typeof(Performance),
                                        new PropertyMetadata(PerformanceType.Normal));

        private void UpdateImage()
        {
            if (AvailableHeight == 0 || AvailableWidth == 0) return;
            FastBuffer = new byte[(int) (AvailableWidth)*(int) (AvailableHeight)];
            writeableBitmap = new WriteableBitmap((int) AvailableWidth, (int) AvailableHeight, 96.0, 96.0,
                PixelFormats.Pbgra32, null);
            bitmapImage.Source = writeableBitmap;
            isImageAdded = true;
        }

#endif



        protected Brush GetBrush()
        {
            Brush brush;
            if (SegmentBrush == null)
            {
                brush = SegmentBrushConverter.GetBrush(Area.UISeries.IndexOf(this));
            }
            else
                brush = SegmentBrush;
            return brush;
        }

#if WPF
        public override void OnApplyTemplate()
#else
        protected override void OnApplyTemplate() 
#endif
        {
            base.OnApplyTemplate();
            SegmentPanel.Children.Add(bitmapImage);
        }

#if WPF_UN

        protected internal override void OnDrawSegment()
        {
            if (RenderPerformance == PerformanceType.Fast)
                UpdateImage();
        }

        protected override Size MeasureOverride(Size constraint)
        {
           
            if (RenderPerformance == PerformanceType.Fast && PointsGenerated && constraint.Width != 0d && constraint.Height != 0d)
            {
                AvailableWidth = constraint.Width;
                AvailableHeight = constraint.Height;

                if (XAxis != null)
                {
                    MinimumX = XAxis.Start;
                    MaximumX = XAxis.End;
                }
                if (YAxis != null)
                {
                    MinimumY = YAxis.Start;
                    MaximumY = YAxis.End;
                }

                OnDrawSegment();
            }
            return base.MeasureOverride(constraint);
        }

#endif
    }

    public enum PerformanceType
    {
        Fast,
        Normal
    }
}
